﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code.Security;
using PlanProd.Contracts.Search;
using Web.Framework;

namespace WebAppPlanProduccion.App
{
    public partial class ReportServerEdit : BasePage
    {
        public ReportServer reportServerCurrent
        {
            get
            {
                return (ReportServer)ViewState["ReportServer"];
            }
            set
            {
                ViewState["ReportServer"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(PlanProdSecurity.ADMINISTRAR_REPORTSERVER);

                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["Id"] == null)
                    {
                        reportServerCurrent = new ReportServer();
                    }
                    else
                    {
                        var id = Convert.ToInt32(this.Request.QueryString["Id"]);
                        reportServerCurrent = WebServiceHelper.GetReportServers(new SearchReportServer() { Id = id }).Single();
                    }

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton guardarButton = new LinkButton();
        LinkButton cancelarButton = new LinkButton();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = "Guardar";
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);

            cancelarButton.ID = "BoCommandCancelar";
            cancelarButton.Text = "Cancelar";
            cancelarButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(cancelarButton);

            base.LoadContextActions(actionButtons);
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();
            guardarButton.Visible =
            lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.ADMINISTRAR_REPORTSERVER);
        }

        private void Bind()
        {
            if (reportServerCurrent.Id != 0)
            {
                this.txtId.Text = Convert.ToString(reportServerCurrent.Id);
                this.txtUrl.Text = reportServerCurrent.Url;
                this.txtDomain.Text = reportServerCurrent.Domain;
                this.txtUserName.Text = reportServerCurrent.UserName;
                //this.txtPassword.Text = reportServerCurrent.LocaleKey;               
            }
        }

        private void SetData()
        {
            reportServerCurrent.Url = this.txtUrl.Text;
            reportServerCurrent.Domain = this.txtDomain.Text;
            reportServerCurrent.UserName = this.txtUserName.Text;
            if (!String.IsNullOrEmpty(this.txtPassword.Text))
            {
                reportServerCurrent.Password = this.txtPassword.Text;
            }
        }

        private void Save()
        {
            reportServerCurrent = WebServiceHelper.SaveReportServer(reportServerCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                BasePage.ClearReportingConfiguration(this.Application);
                Response.Redirect("/App/ReportServerList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/App/ReportServerList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}